﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Auros.Categorisation.WebControls
{
    public class CategorySelectTree : Control
    {
        protected override void CreateChildControls()
        {
            //base.CreateChildControls();

            ;

            HtmlGenericControl wrapper = new HtmlGenericControl("div");
            wrapper.Attributes.Add("class", "standard tree tree-umbraco umbTree");

            wrapper.Controls.Add(this.CreateTree());

            this.Controls.Add(wrapper);
        }

        private Control CreateTree(int parentId = -1)
        {
            HtmlGenericControl ulControl = new HtmlGenericControl("ul");

            Category[] categories = null;

            if(parentId > 0)
            {
                categories = Categories.GetCategories(parentId);
            }
            else
            {
                categories = Categories.GetCategories();
            }

            foreach (var category in categories)
            {
                HtmlGenericControl li = new HtmlGenericControl("li");

                HtmlGenericControl span = new HtmlGenericControl("span");

                li.Controls.Add(span);

                if (category.HasChildren)
                {
                    li.Controls.Add(this.CreateTree(category.ID));
                }

                ulControl.Controls.Add(li);
            }

            return ulControl;
        }
    }
}